package com.vortex.cloud.sample.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vortex.cloud.sample.domain.StatsInfo;
import com.vortex.cloud.sample.dto.StatsInfoDTO;
import com.vortex.cloud.sample.dto.StatsInfoQueryDTO;
import com.vortex.cloud.sample.mapper.StatsInfoMapper;
import com.vortex.cloud.sample.service.StatsInfoService;
import com.vortex.cloud.sample.vo.StatsInfoVo;
import com.vortex.cloud.vfs.lite.base.dto.DataStoreDTO;
import com.vortex.cloud.vfs.lite.data.util.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-10-21 周四
 * @time 14:06
 * @description
 */
@Service
public class StatsInfoServiceImpl extends ServiceImpl<StatsInfoMapper, StatsInfo> implements StatsInfoService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer save(String tenantId, StatsInfoDTO dto) {
        //自定义注解验证
//        try {
//            MyAnnotationUtils.validateParameter(dto);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return -1;
//        }

        Assert.notNull(dto.getTaskDate(), "日期不能为空哦！");
        StatsInfo statsInfo = new StatsInfo();
        BeanUtils.copyProperties(dto, statsInfo);
        statsInfo.setTenantId(tenantId);
        return baseMapper.insert(statsInfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer update(String tenantId, StatsInfoDTO dto) {
        StatsInfo statsInfo = new StatsInfo();
        BeanUtils.copyProperties(dto, statsInfo);
        LambdaQueryWrapper<StatsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StatsInfo::getTenantId, tenantId);

        return baseMapper.update(statsInfo, lqw);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delete(Set<String> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    @Transactional(readOnly = true)
    @Override
    public DataStoreDTO<StatsInfoVo> page(String tenantId, Pageable pageable, StatsInfoQueryDTO params) {
        LambdaQueryWrapper<StatsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(params.getTaskDate()), StatsInfo::getTaskDate, params.getTaskDate());
//                .eq(Objects.nonNull(dto.getTaskTime()), StatsInfo::getTaskTime, dto.getTaskTime())
//                .eq(Objects.nonNull(dto.getTaskDateTime()), StatsInfo::getTaskDateTime, dto.getTaskDateTime())
//                .eq(Objects.nonNull(dto.getAwarenessRate()), StatsInfo::getAwarenessRate, dto.getAwarenessRate())
//                .eq(Objects.nonNull(dto.getParticipationRate()), StatsInfo::getParticipationRate, dto.getParticipationRate())
//                .eq(Objects.nonNull(dto.getAccuracyRate()), StatsInfo::getAccuracyRate, dto.getAccuracyRate())
//                .eq(Objects.nonNull(dto.getSatisfaction()), StatsInfo::getSatisfaction, dto.getSatisfaction())
//                .eq(Objects.nonNull(dto.getReductionRate()), StatsInfo::getReductionRate, dto.getReductionRate())
//                .eq(Objects.nonNull(dto.getProcessRate()), StatsInfo::getProcessRate, dto.getProcessRate())
//                .eq(Objects.nonNull(dto.getUseRate()), StatsInfo::getUseRate, dto.getUseRate())
//                .eq(Objects.nonNull(dto.getCarbonEmissions()), StatsInfo::getCarbonEmissions, dto.getCarbonEmissions());
        Page<StatsInfo> page = PageUtils.transferPage(pageable);
        Page<StatsInfo> result = baseMapper.selectPage(page, lqw);

        List<StatsInfoVo> taskVOList = result.getRecords().stream().map(entity -> {
            StatsInfoVo vo = new StatsInfoVo();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
        return new DataStoreDTO(result.getTotal(), taskVOList);
    }
}
